package cn.bdqn.hospital.dao.impl;

import cn.bdqn.hospital.dao.DrugDoseDao;
import cn.bdqn.hospital.entity.DrugDose;
import cn.bdqn.hospital.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class DrugDoseDaoImpl implements DrugDoseDao {
    QueryRunner queryRunner = new QueryRunner();

    /**
     * 查询所有剂量
     *
     * @return
     * @throws SQLException
     */
    @Override
    public List<DrugDose> getAll() throws SQLException {
        List<DrugDose> drugDoseList =null;
        String sql="select * from drugdose;";
        drugDoseList =queryRunner.query(DruidUtils.getConnection(),sql,new BeanListHandler<DrugDose>(DrugDose.class));
        return drugDoseList;
    }

    /**
     * 通过id查询剂量
     *
     * @param drugId
     * @return
     * @throws SQLException
     */
    @Override
    public DrugDose getDrugDoseById(Integer drugId) throws SQLException {
        DrugDose drugDose=null;
        String sql="select * from drugdose where drugId=?;";
        drugDose=queryRunner.query(DruidUtils.getConnection(),sql,new BeanHandler<DrugDose>(DrugDose.class),drugId);
        return drugDose;
    }

    /**
     * 新增剂量
     *
     * @param drugDose
     * @return
     * @throws SQLException
     */
    @Override
    public int add(DrugDose drugDose) throws SQLException {
        int num =0;
        String sql="insert into drugdose(drugDoseName) value(?);";
        num=queryRunner.update(DruidUtils.getConnection(),sql,drugDose.getDrugDoseName());
        return num;
    }

    /**
     * 修改剂量
     *
     * @param drugDose
     * @return
     * @throws SQLException
     */
    @Override
    public int update(DrugDose drugDose) throws SQLException {
        int num=0;
        String sql ="update drugdose set drugDoseName=? where drugId=?;";
        num =queryRunner.update(DruidUtils.getConnection(),sql, drugDose.getDrugId());
        return num;
    }

    /**
     * 通过id删除
     *
     * @param drugId
     * @return
     * @throws SQLException
     */
    @Override
    public int delete(Integer drugId) throws SQLException {
        int num =0;
        String sql="delete from drugdose where drugid=?;";
        num = queryRunner.update(DruidUtils.getConnection(),sql,drugId);
        return num;
    }
}
